/*
 * simulator.h
 *
 *  Created on: Jun 25, 2009
 *      Author: matt
 */

#ifndef SIMULATOR_H_
#define SIMULATOR_H_
#include "robot.h"
#include "constants.h"
#include "mathutil.h"
#include <ctime>

typedef struct tagCOLLISIONTUPLE {
	LINE2DF robot_line;
	LINE2DF env_line;
} COLLISION_TUPLE;

typedef struct tagCOLLISIONDATA {
	unsigned int num;
	unsigned int unique_env_lines_num;
	vector<COLLISION_TUPLE> clines;
	vector<VECTOR2DF> cpoints;
} COLLISION_DATA;

class SIMULATOR {
	friend class CONSOLE;
private:
	ROBOT * robot;
	SIMULOBJECT * env;
	unsigned int mode;
	COLLISION_DATA cdata;
	vector<COLLISION_DATA> laserdata;
	clock_t lastclock;

private:
	void ClearCollisionData();
	bool Collision();
	void Init();
public:
	static const int WALLDRAG = 0x1;
	static const int COMPREHENSIVE_COLLISION = 0x2;
	static const int LASER_COLLISION = 0x4;
	static const int SIM_MOTOR_MODE = 0x8;

	SIMULATOR();
	SIMULATOR(int mode);
	virtual ~SIMULATOR();

	void SetEnv(SIMULOBJECT* o);
	void SetRobot(ROBOT* o);
	void SetMode(unsigned int mode);
	void SetMotorMode(bool);

	void RobotTurnRight();
	void RobotTurnLeft();
	void RobotMoveForward();
	void RobotMoveBackward();
	void RobotUseLaser();

	void GetCollisionData(COLLISION_DATA& cd);
	void Run();

};

//bool line_sort_x(LINE2DF& l1, LINE2DF& l2){
//	VECTOR2DF e1, e2, e3, e4;
//	endpoints(l1, e1, e2);
//	endpoints(l2, e3, e4);
//	float hx1 = e1.x < e2.x ? e1.x : e2.x;
//	float hx2 = e3.x < e4.x ? e3.x : e4.x;
//	return hx1 < hx2;
//}
//
//bool line_sort_y(LINE2DF& l1, LINE2DF& l2){
//	VECTOR2DF e1, e2, e3, e4;
//	endpoints(l1, e1, e2);
//	endpoints(l2, e3, e4);
//	float hy1 = e1.y < e2.y ? e1.y : e2.y;
//	float hy2 = e3.y < e4.y ? e3.y : e4.y;
//	return hy1 < hy2;
//}
//


#endif /* SIMULATOR_H_ */
